package com.zf.zfed.audit.ui

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zf.zfed.audit.R

/**
 * 密码管理卡片组件
 */
class PasswordManagementCard @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    
    private var passwordIcon: ImageView
    private var passwordText: TextView
    private var modifyButton: PasswordActionButton
    private var resetButton: PasswordActionButton
    private var unlockButton: PasswordActionButton
    
    init {
        orientation = HORIZONTAL
        isClickable = true
        isFocusable = true
        
        // 设置卡片背景
        background = ContextCompat.getDrawable(context, R.drawable.card_background)
        
        // 设置卡片边距
        val cardPadding = context.dpFromDimen(R.dimen.spacing_8)
        setPadding(cardPadding, cardPadding, cardPadding, cardPadding)
        
        // 左侧图标和文字
        val leftContainer = FrameLayout(context)
        val leftLP = LayoutParams(0, LayoutParams.MATCH_PARENT, 1f)
        addView(leftContainer, leftLP)
        
        // 密码图标
        passwordIcon = ImageView(context)
        val iconSize = context.dpFromDimen(R.dimen.size_24)
        val iconLP = FrameLayout.LayoutParams(iconSize, iconSize)
        iconLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        iconLP.leftMargin = context.dpFromDimen(R.dimen.spacing_8)
        passwordIcon.setImageResource(R.drawable.u3227)
        passwordIcon.setColorFilter(ContextCompat.getColor(context, R.color.text_primary))
        leftContainer.addView(passwordIcon, iconLP)
        
        // 密码文字
        passwordText = TextView(context)
        passwordText.setTextColor(ContextCompat.getColor(context, R.color.text_primary))
        passwordText.textSize = context.spFromDimen(R.dimen.font_16)
        passwordText.setTypeface(null, android.graphics.Typeface.BOLD)
        passwordText.text = "密码"
        val textLP = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        textLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        textLP.leftMargin = context.dpFromDimen(R.dimen.spacing_40)
        leftContainer.addView(passwordText, textLP)
        
        // 右侧按钮容器
        val rightContainer = LinearLayout(context)
        rightContainer.orientation = HORIZONTAL
        rightContainer.gravity = Gravity.CENTER_VERTICAL
        val rightLP = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        addView(rightContainer, rightLP)
        
        // 修改按钮
        modifyButton = PasswordActionButton(context)
        modifyButton.setActionIcon(R.drawable.u1761)
        modifyButton.setActionText("修改")
        val modifyLP = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        modifyLP.rightMargin = context.dpFromDimen(R.dimen.spacing_8)
        rightContainer.addView(modifyButton, modifyLP)
        
        // 重置按钮
        resetButton = PasswordActionButton(context)
        resetButton.setActionIcon(R.drawable.u3238)
        resetButton.setActionText("重置")
        val resetLP = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        resetLP.rightMargin = context.dpFromDimen(R.dimen.spacing_8)
        rightContainer.addView(resetButton, resetLP)
        
        // 解锁按钮
        unlockButton = PasswordActionButton(context)
        unlockButton.setActionIcon(R.drawable.u3239)
        unlockButton.setActionText("解锁")
        val unlockLP = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        rightContainer.addView(unlockButton, unlockLP)
    }
    
    /**
     * 设置按钮是否可用
     */
    fun setButtonsEnabled(enabled: Boolean) {
        modifyButton.isEnabled = enabled
        resetButton.isEnabled = enabled
        unlockButton.isEnabled = enabled
    }
    
    /**
     * 设置按钮是否可见
     */
    fun setButtonsVisible(visible: Boolean) {
        modifyButton.visibility = if (visible) VISIBLE else GONE
        resetButton.visibility = if (visible) VISIBLE else GONE
        unlockButton.visibility = if (visible) VISIBLE else GONE
    }
    
    /**
     * 设置修改按钮点击监听器
     */
    fun setOnModifyClickListener(listener: () -> Unit) {
        modifyButton.setOnClickListener { listener() }
    }
    
    /**
     * 设置重置按钮点击监听器
     */
    fun setOnResetClickListener(listener: () -> Unit) {
        resetButton.setOnClickListener { listener() }
    }
    
    /**
     * 设置解锁按钮点击监听器
     */
    fun setOnUnlockClickListener(listener: () -> Unit) {
        unlockButton.setOnClickListener { listener() }
    }
}

